// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.1

package fileTag

import (
	"audio-server/internal/model"
	"audio-server/internal/sqlutil"
	"context"
	"errors"

	"audio-server/internal/svc"
	"audio-server/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type FileTagUpdateLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewFileTagUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FileTagUpdateLogic {
	return &FileTagUpdateLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *FileTagUpdateLogic) FileTagUpdate(req *types.FileTagSaveReq) (resp bool, err error) {
	id := sqlutil.GetUserId(l.ctx)
	if id == 0 {
		return false, errors.New("请先登录")
	}
	tagModel := model.NewSysFileTagModel(l.svcCtx.Conn)
	_, err = tagModel.FindByName(l.ctx, req.Title, id)
	if err != nil {
		return false, errors.New("已存在该标签")
	}
	oldTag, err := tagModel.FindOne(l.ctx, req.TagId)
	if err != nil {
		return false, err
	}
	oldTag.Title = req.Title
	err = tagModel.Update(l.ctx, oldTag)
	if err != nil {
		return false, err
	}

	return true, nil
}
